﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.RaiseManagement;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.RaiseCommands
{
    public class EliminateSelectHandler : IRequestHandler<EliminateSelectCommand, Result>
    {
        private readonly IBaseRepository<SheepCulled> sheepculledRep;
        private readonly IBaseRepository<Culled> culledRep;
        private readonly IBaseRepository<SleepInfor> sleepInforRep;
        private readonly IBaseRepository<Cottages> cottagesRep;
        private readonly IBaseRepository<Fields> fieldsRep;
        private readonly IBaseRepository<Variety> varietyRep;

        public EliminateSelectHandler(IBaseRepository<SheepCulled> sheepculledRep, IBaseRepository<Culled> culledRep, IBaseRepository<SleepInfor> sleepInforRep, IBaseRepository<Cottages> cottagesRep, IBaseRepository<Fields> fieldsRep, IBaseRepository<Variety> varietyRep)
        {
            this.sheepculledRep = sheepculledRep;
            this.culledRep = culledRep;
            this.sleepInforRep = sleepInforRep;
            this.cottagesRep = cottagesRep;
            this.fieldsRep = fieldsRep;
            this.varietyRep = varietyRep;
        }


        /// <summary>
        /// 淘汰显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(EliminateSelectCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.success };

            var list = (from a in sleepInforRep.GetAll()
                        join b in sheepculledRep.GetAll() on a.SleepInforId equals b.SleepInforId
                        join c in culledRep.GetAll() on b.CulledId equals c.CulledId
                        join d in cottagesRep.GetAll() on a.RecordcottageId equals d.CottagesId
                        join e in fieldsRep.GetAll() on a.FieIdId equals e.FieIdId
                        join f in varietyRep.GetAll() on a.VarietyId equals f.VarietyId
                        select new EliminateSelectDto()
                        {
                            SheepCulledId = b.SheepCulledId,
                            CulledDate = b.CulledDate,
                            SleepInforId = a.SleepInforId,
                            EarNumber = a.EarNumber,
                            CulledId = c.CulledId,
                            CulledName = c.CulledName,
                            DocumentNum = b.DocumentNum,
                            ExitName = b.ExitName,
                            Measures = b.Measures,
                            VarietyId = f.VarietyId,
                            VarietyName = f.VarietyName,
                            Gender = a.Gender,
                            BreedStatuId = a.BreedStatuId,
                            CottagesId = d.CottagesId,
                            CottageName = d.CottageName,
                            FieIdId = e.FieIdId,
                            FieIdName = e.FieIdName,
                            SleepBirthDate = a.SleepBirthDate,
                            CreateId = b.CreateId,
                            CreateTime = b.CreateTime,
                            UpdateId = b.UpdateId,
                            UpdateTime = b.UpdateTime,
                            ExamId = b.ExamId,
                            ExamTime = b.ExamTime,
                            AgeDay = (int)(DateTime.Now - Convert.ToDateTime(b.CulledDate)).TotalDays,
                        });
            if (!string.IsNullOrEmpty(request.start))
            {
                list = list.Where(x => x.CulledDate >= DateTime.Parse(request.start));
            }
            if (!string.IsNullOrEmpty(request.end))
            {
                list = list.Where(x => x.CulledDate < DateTime.Parse(request.end));
            }
            if (request.sleepInforId > 0)
            {
                list = list.Where(x => x.SleepInforId == request.sleepInforId);
            }
            if (request.culledId > 0)
            {
                list = list.Where(x => x.CulledId == request.culledId);
            }
            if (request.ExitName > 0)
            {
                list = list.Where(x => x.ExitName == request.ExitName);
            }



            if (list != null)
            {
                result.Code = (int)CodeEnum.success;
                result.Data = list;
            }

            return result;
        }
    }
}
